package com.jy.wms.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.CommonInfo;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.dao.WmsShipmentOrderDetailedMapper;
import com.jy.wms.dao.WmsShipmentOrderMapper;
import com.jy.wms.pojo.WmsShipmentOrder;
import com.jy.wms.pojo.WmsShipmentOrderDetailed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: WmsShipmentOrderDetailedService.java
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author LH
 * @Date Wed Oct 18 14:53:24 GMT+08:00 2017
 */

@Service
public class WmsShipmentOrderDetailedService extends BaseService<WmsShipmentOrderDetailedMapper, WmsShipmentOrderDetailed> {
    @Autowired
    private WmsShipmentOrderMapper wmsShipmentOrderMapper;


    /**
     * 插入一条记录
     *
     * @param entity 实体对象
     * @return int
     */
    @BaseServiceDescribe(moduleName = "发运订单", btnName = "增加订单")

    public Integer baseInsert(WmsShipmentOrderDetailed entity) throws SucessException, ServiceException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Date d = new Date();
        String date = df.format(d);// new Date()为获取当前系统时间
        entity.setCjsj(date);
        entity.setId(UUIDUtils.uuid());
        if(entity.getScrq()!=null && !entity.getScrq().equals("")){
            entity.setScrq(df.format(new Date(Long.valueOf(entity.getScrq()).longValue())));
        }
        //查询当前发运订单下是否有相同的货品
        Integer a = dao.getMxCountByDdid(entity.getSsfyddid(), entity.getHpid(), "");
        if (a > 0) {
            throw new ServiceException("当前发运订单下包含重复货品，修改失败");
        } else {
            //增加明细
            Integer b = dao.baseInsertSelective(entity);
            if (b > 0) {
                throw new SucessException("订单明细增加成功");
            } else {
                throw new ServiceException("订单明细增加失败");
            }
        }
    }


    /**
     * 修改订单明细 （匹配有值的字段）
     *
     * @param entity 主键ID列表
     * @return int
     */
    @BaseServiceDescribe(moduleName = "发运订单", btnName = "修改发运订单明细")
    public Integer baseUpdateMx(WmsShipmentOrderDetailed entity) throws SucessException, ServiceException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Date d = new Date();
        String date = df.format(d);// new Date()为获取当前系统时间
        if(entity.getScrq()!=null && !entity.getScrq().equals("")){
            entity.setScrq(df.format(new Date(Long.valueOf(entity.getScrq()).longValue())));
        }
        WmsShipmentOrder wmsShipmentOrder = wmsShipmentOrderMapper.baseSelectByPrimaryKey(entity.getSsfyddid());//通过发运订单id查询对象
        if (wmsShipmentOrder.getCdzt().equals("1")) {
            throw new ServiceException("订单已拆单，不可以进行编辑");
        } else {
            //查询当前发运订单下是否有相同的货品(如果货品更改了，那么要在数据库查找是否有相同的货品
            // （查询的时候排除当前修改的这条明细，因为当前明细要被更改））)
            Integer a = dao.getMxCountByDdid(entity.getSsfyddid(), entity.getHpid(), entity.getId());
            if (a > 0) {
                throw new ServiceException("当前发运订单下包含重复货品，修改失败");
            } else {
                //修改明细
                Integer b = dao.baseUpdateByPrimaryKeySelective(entity);
                if (b > 0) {
                    throw new SucessException("订单明细修改成功");
                } else {
                    throw new ServiceException("订单明细修改失败");
                }
            }
        }
    }

    /**
     * 删除订单明细 （匹配有值的字段）
     *
     * @param idList   明细id集合
     * @param fyddidid 订单id
     * @return int
     */
    @BaseServiceDescribe(moduleName = "发运订单", btnName = "删除发运订单明细")
    public Integer baseDeleteByPrimaryKey(List<String> idList, String fyddidid) throws SucessException, ServiceException {
        WmsShipmentOrder wmsShipmentOrder = wmsShipmentOrderMapper.baseSelectByPrimaryKey(fyddidid);//通过发运订单id查询对象
        //查询当前发运订单下的明细数量，如果只剩下一条明细那么不可以删除
        Integer count = dao.selectCount(new EntityWrapper<WmsShipmentOrderDetailed>().eq("ssfyddid", fyddidid));
        if (count == 1) {
            throw new ServiceException("此订单只包含一条明细，不可以删除");
        } else {
            if (wmsShipmentOrder.getCdzt().equals(CommonInfo.shipmentOrder_ycd.getType())) {
                throw new ServiceException("发运订单已拆单，不可以进行删除");
            } else {
                Integer a = dao.deleteBatchIds(idList);
                if (a > 0) {
                    throw new SucessException("订单明细删除成功");
                } else {
                    throw new ServiceException("订单明细删除失败");
                }
            }
        }
    }


    /**
     * 插入一条记录
     *
     * @param entity 实体对象
     * @return int
     */
    public Integer baseInsert(List<WmsShipmentOrderDetailed> entity) {
        return dao.baseInsert(entity);
    }

    /**
     * 插入一条记录（匹配有值的字段）
     *
     * @param entity 实体对象
     * @return int
     */
    public Integer baseInsertSelective(WmsShipmentOrderDetailed entity) {
        return dao.baseInsertSelective(entity);
    }

    /**
     * 删除（根据主键ID删除）
     *
     * @param id 主键
     * @return int
     */
    public Integer baseDeleteByPrimaryKey(String id) {
        return dao.baseDeleteByPrimaryKey(id);
    }

    /**
     * 删除（根据ID 批量删除）
     *
     * @param idList 主键ID列表
     * @return int
     */
    public Integer baseDeleteBathPrimaryKeys(List<String> idList) {
        return dao.baseDeleteBathPrimaryKeys(idList);
    }

    /**
     * 分页查询
     *
     * @param entity 实体
     * @return Page<WmsShipmentOrderDetailed>
     */
    public Page<WmsShipmentOrderDetailed> baseSelectPage(WmsShipmentOrderDetailed entity) {
        PageHelper.startPage(entity.getPage(), entity.getRows());
        return (Page<WmsShipmentOrderDetailed>) dao.baseSelectPage(entity);
    }

    /**
     * 查询（根据主键ID查询）
     *
     * @param id 主键ID
     * @return WmsShipmentOrderDetailed
     */
    public WmsShipmentOrderDetailed baseSelectByPrimaryKey(String id) {
        return dao.baseSelectByPrimaryKey(id);
    }

    /**
     * 查询（根据ID 批量查询）
     *
     * @param idList 主键ID列表
     * @return list
     */
    public Integer baseSelectBathPrimaryKeys(List<WmsShipmentOrderDetailed> idList) {
        return dao.baseSelectBathPrimaryKeys(idList);
    }

    /**
     * 修改 （匹配有值的字段）
     *
     * @param entity 主键ID列表
     * @return int
     */
    public Integer baseUpdateByPrimaryKeySelective(WmsShipmentOrderDetailed entity) {
        return dao.baseUpdateByPrimaryKeySelective(entity);
    }

    /**
     * 修改（根据主键ID修改）
     *
     * @param entity 主键ID列表
     * @return int
     */
    public Integer baseUpdateByPrimaryKey(WmsShipmentOrderDetailed entity) {
        return dao.baseUpdateByPrimaryKey(entity);
    }
}
